<script setup lang="ts">
//样片类型表单组件
import type {Ref} from "vue-demi";
import {type TableSampleDto, addSampleType, updateSampleType} from "@/apis/sample/class";
import {useUserStore} from "@/stores";


const users = useUserStore().userInfo

const emit = defineEmits(["edit", "add"])

const visible = defineModel('visible', {
  default: false,
  type: Boolean
})

const formData:Ref<TableSampleDto> = defineModel('formData', {
  default: () => ({
    id: undefined,
    sampleClassName: '',
    status: 1,
    updateBy: ""
  }),
  type: Object
})

const handleSubmit = () => {

  if (formData.value.id === 0) {
    addSampleType(formData.value).then(res => {
      visible.value = false
      emit("add")
      clear()
    })
  } else {
    formData.value.updateBy = users.id
    updateSampleType(formData.value).then(res => {
      visible.value = false
      emit("edit")
      clear()
    })
  }
}

const cancel = () => {
  visible.value = false
  clear()
}

const clear = () => {
  formData.value.id = 0
  formData.value.sampleClassName = ""
  formData.value.updateBy = users.id
  formData.value.status = 1
}

</script>

<template>
  <a-modal v-model:visible="visible" :title="formData.id !== 0 ? '编辑类型' : '新增类型'">
    <a-form :model="formData" layout="vertical">
      <a-form-item label="类型名称" field="sampleClassName" required>
        <a-input v-model="formData.sampleClassName" placeholder="请输入类型名称" />
      </a-form-item>
      <a-form-item label="状态" field="status" required>
        <a-select v-model="formData.status">
          <a-option :value="1">启用</a-option>
          <a-option :value="0">停用</a-option>
        </a-select>
      </a-form-item>
    </a-form>
    <template #footer>
      <a-button @click="cancel">取消</a-button>
      <a-button type="primary" @click="handleSubmit">确认</a-button>
    </template>
  </a-modal>
</template>

<style scoped lang="scss">

</style>